<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'delete', 'comSearch', 'quickSearch']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('shop.notify.notify.quick Search Fields') })"
        >
            <el-popconfirm title="确定将所选消息标为已读吗?" @confirm="onBatchRead" width="230">
                <template #reference>
                    <el-button
                        v-blur
                        class="table-header-operate"
                        style="margin-left: 12px"
                        type="info"
                        :disabled="baTable.getSelectionIds().length <= 0"
                    >
                        <Icon color="#ffffff" name="el-icon-View" />
                        <span class="table-header-operate-text">标为已读</span>
                    </el-button>
                </template>
            </el-popconfirm>
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #message>
                <el-table-column show-overflow-tooltip headerAlign="center" :label="t('shop.notify.notify.message')" min-width="300">
                    <template #default="{ row }">
                        <el-tag class="status" v-if="row.status == 0" type="danger">{{ t('shop.notify.notify.status 0') }}</el-tag>
                        <el-tag class="status" v-else-if="row.status == 1" type="info">{{ t('shop.notify.notify.status 1') }}</el-tag>
                        <el-text :class="row.status == 1 ? 'read' : 'unread'" @click.once="onHandleMessage(row)">{{ row.message }}</el-text>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import { handleMessage, readMessage } from '/@/api/backend/shop/notify/notify'

defineOptions({
    name: 'shop/notify/notify',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['delete'])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/shop.notify.Notify/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            {
                label: t('shop.notify.notify.message'),
                prop: 'message',
                align: 'left',
                headerAlign: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: false,
                sortable: false,
                render: 'slot',
                slotName: 'message',
            },
            {
                label: t('shop.notify.notify.status'),
                prop: 'status',
                operator: '=',
                sortable: false,
                render: 'tag',
                align: 'center',
                custom: { '0': 'danger', '1': 'info' },
                replaceValue: { '0': t('shop.notify.notify.status 0'), '1': t('shop.notify.notify.status 1') },
                width: '80',
                show: false,
            },
            {
                label: t('shop.notify.notify.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: {},
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
const onHandleMessage = (row: TableRow) => {
    if (row.status == 1) return
    row.status = 1
    handleMessage(row.id, row.message_type, row.message_data)
}

const onBatchRead = () => {
    const ids = baTable.getSelectionIds()

    readMessage(ids).then(() => {
        baTable.onTableHeaderAction('refresh', {})
    })
}
</script>

<style scoped lang="scss">
.status {
    margin-right: 5px;
}
.unread {
    font-weight: bold;
    cursor: pointer;

    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.read {
    color: #999;
}
</style>
